package com.otp.controller;

import com.otp.pojo.dto.CourseJoinDto;
import com.otp.pojo.entity.CourseJoin;
import com.otp.pojo.request.course.*;
import com.otp.util.ResponseMessage;
import com.otp.service.CourseJoinService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/course-join")
public class CourseJoinController {
    @Autowired
    private CourseJoinService courseJoinService;

    @PostMapping
    public ResponseMessage<CourseJoinDto> join(@RequestBody CourseJoinRequest request) {
        return ResponseMessage.success(courseJoinService.joinCourse(request)
                .convertToDto());
    }

    @PostMapping("/review")
    public ResponseMessage<CourseJoinDto> reviewJoin(@RequestBody CourseJoinReviewRequest request) {
        return ResponseMessage.success(courseJoinService.reviewJoin(request)
                .convertToDto());
    }

    @GetMapping
    public ResponseMessage<List<CourseJoinDto>> listAll() {
        return ResponseMessage.success(courseJoinService.listAll()
                .stream()
                .map(CourseJoin::convertToDto)
                .toList());
    }

    @GetMapping("/{id}")
    public ResponseMessage<CourseJoinDto> getById(@PathVariable Long id) {
        return ResponseMessage.success(courseJoinService.getById(id)
                .convertToDto());
    }

    @GetMapping("/course/{courseId}")
    public ResponseMessage<List<CourseJoinDto>> listByCourseId(@PathVariable Long courseId) {
        return ResponseMessage.success(courseJoinService.listByCourseId(courseId)
                .stream()
                .map(CourseJoin::convertToDto)
                .toList());
    }

    @GetMapping("/student/{studentId}")
    public ResponseMessage<List<CourseJoinDto>> listByStudentId(@PathVariable Long studentId) {
        return ResponseMessage.success(courseJoinService.listByStudentId(studentId)
                .stream()
                .map(CourseJoin::convertToDto)
                .toList());
    }

    @GetMapping("/status/{status}")
    public ResponseMessage<List<CourseJoinDto>> listByStatus(@PathVariable String status) {
        return ResponseMessage.success(courseJoinService.listByStatus(status)
                .stream()
                .map(CourseJoin::convertToDto)
                .toList());
    }

    @PutMapping
    public ResponseMessage<CourseJoinDto> update(@RequestBody CourseJoinUpdateRequest request) {
        return ResponseMessage.success(courseJoinService.update(request)
                .convertToDto());
    }

    @DeleteMapping("/{id}")
    public ResponseMessage<?> delete(@PathVariable Long id) {
        courseJoinService.delete(id);
        return null;
    }
}